<?php
if (isset($_POST['editer'])) 
{
	$req = $bdd->prepare('SELECT * FROM medias WHERE id = :id');
	$req->execute(array('id' => $_POST['id']));
	$donnees = $req->fetch();
	?>
	<h1>Editer une image</h1>
	<form method="post" action="index.php?page=admin_medias" enctype="multipart/form-data">
		<table border="0" width="100%">
			<tr>
			<td rowspan="3" width="100"><img src="<?php echo $donnees['lien']; ?>" width="150"></td>
			<td><label for="nom">Nom</label></td>
			<td><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="50" required></td></tr></tr>
			<tr><td colspan="3"><hr/><input type="file" size="150" name="image" /><hr/></td></tr>
			<tr><td colspan="3"><label for="description">Description</label></td></tr>
			<tr><td colspan="3">
			<textarea id="description" name="description" rows="15" cols="80"><?php echo $donnees['description']; ?></textarea>
			</td></tr>
			<tr><td colspan="3"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
			<input type="submit" value="Mettre à jour" name="modifier" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
elseif (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE medias SET nom=:nom, categorie=:categorie, description=:description WHERE id=:id');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'categorie'  => $_POST['type'],
		'description' => stripslashes($_POST['description']),
		'id' => $_POST['id']
	));
	$type=$_POST['type'];
	switch ($type)
	{
		case '1': 
			if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
			{
				if ($_FILES['image']['size'] <= 1000000)
				{
					$infosfichier = pathinfo($_FILES['image']['name']);
					$extension_upload = $infosfichier['extension'];
					$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
					if (in_array($extension_upload, $extensions_autorisees))
					{
						$chemin = 'images/'.$nom_media.'.'.$extension_upload;
						move_uploaded_file($_FILES['image']['tmp_name'], $chemin);
						
						$req = $bdd->prepare('UPDATE medias SET lien=:lien WHERE id=:id');
						$req->execute(array(
							'lien' => $chemin,
							'id' => $_POST['id']
						));
					}
				}
			}
		break;
		
		case '2': 
			if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
			{
				if ($_FILES['image']['size'] <= 1000000)
				{
					$infosfichier = pathinfo($_FILES['image']['name']);
					$extension_upload = $infosfichier['extension'];
					$extensions_autorisees = array('pdf', 'PDF');
					if (in_array($extension_upload, $extensions_autorisees))
					{
						$chemin = 'pdf/'.$nom_media.'.'.$extension_upload;
						move_uploaded_file($_FILES['image']['tmp_name'], $chemin);
						
						$req = $bdd->prepare('UPDATE medias SET lien=:lien WHERE id=:id');
						$req->execute(array(
							'lien' => $chemin,
							'id' => $_POST['id']
						));
					}
				}
			}
		break;
	}
	header('Location: index.php?page=admin_medias'); 
}
elseif (isset($_POST['effacer'])) 
{
	$fichier=$_POST['lien'];
	//echo $fichier;
	if (!unlink($fichier)) {
		die('Echec lors de la destruction de l\'image (fichier).');
	}
	else
	{
		$req = $bdd->prepare('DELETE FROM medias WHERE id=:id');
		$req->execute(array('id' => $_POST['id']));
		header('Location: index.php?page=admin_medias'); 
	}
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->query("SELECT MAX(id) AS nom_media FROM medias");
	$donnees = $req->fetch();
	$nom_media=$donnees['nom_media']+1;
	$type=$_POST['type'];
	switch ($type)
	{
		case '1': 
			if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
			{
				if ($_FILES['image']['size'] <= 1000000)
				{
					$infosfichier = pathinfo($_FILES['image']['name']);
					$extension_upload = $infosfichier['extension'];
					$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
					if (in_array($extension_upload, $extensions_autorisees))
					{
						$chemin = 'images/'.$nom_media.'.'.$extension_upload;
						move_uploaded_file($_FILES['image']['tmp_name'], $chemin);
						
						$req = $bdd->prepare('INSERT INTO medias (nom, categorie, description, lien) VALUES (:nom, :categorie, :description, :lien)');
						$req->execute(array(
							'nom' => stripslashes($_POST['nom']),
							'categorie'  => $_POST['type'],
							'description' => stripslashes($_POST['description']),
							'lien' => $chemin
						));
					}
				}
			}
		break;
		
		case '2': 
			if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
			{
				if ($_FILES['image']['size'] <= 1000000)
				{
					$infosfichier = pathinfo($_FILES['image']['name']);
					$extension_upload = $infosfichier['extension'];
					$extensions_autorisees = array('pdf', 'PDF');
					if (in_array($extension_upload, $extensions_autorisees))
					{
						$chemin = 'pdf/'.$nom_media.'.'.$extension_upload;
						move_uploaded_file($_FILES['image']['tmp_name'], $chemin);
						
						$req = $bdd->prepare('INSERT INTO medias (nom, categorie, description, lien) VALUES (:nom, :categorie, :description, :lien)');
						$req->execute(array(
							'nom' => stripslashes($_POST['nom']),
							'categorie'  => $_POST['type'],
							'description' => stripslashes($_POST['description']),
							'lien' => $chemin
						));
					}
				}
			}
		break;
	}
	header('Location: index.php?page=admin_medias');
}
elseif (isset($_POST['nouveau'])) 
{
	?>
	<h1>Gestionnaire de médias</h1>
	<form method="post" action="index.php?page=admin_medias" enctype="multipart/form-data">
		<table border="0" width="100%">
			<tr>
			<td><label for="nom">Nom</label></td>
			<td><input type="text" name="nom" value="" maxlength="250" size="50" required></td></tr>
			<td><input type="radio" name="type" value="1" id="type" checked /></td>
			<td><label for="type">Image</label></td></tr>
			<td><input type="radio" name="type" value="2" id="type" /></td>
			<td><label for="type">PDF</label></td></tr>
			<tr><td colspan="2"><hr/><input type="file" size="150" name="image" /><hr/></td></tr>
			<tr><td><label for="description">Description</label></td>
			<td colspan="2"><input type="text" name="description" value="" maxlength="250" size="50"></td></tr>
			<tr><td colspan="2"><input type="submit" value="Ajouter" name="ajouter" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
else
{
	?>
	<form method="post" action="index.php?page=admin_medias">
		<input type="submit" value="Nouveau" name="nouveau" class="bouton" />
	</form>
	<?php
	$req = $bdd->query('SELECT * FROM medias ORDER BY id DESC');
	?>
	<h1>Médias disponibles</h1>
	<div class="colonne-deux">
		<?php
		while ($donnees = $req->fetch())
		{
			?>
			<form method="post" action="index.php?page=admin_medias">
				<p>
				<b><?php echo stripslashes($donnees['nom']); ?></b><br/>
				<?php
				$type=$donnees['categorie'];
				switch ($type)
				{
					case '1': 
						?>
						<img src="<?php echo $donnees['lien']; ?>" width="80" class="img-gauche">
						<?php
					break;
					case '2':
						?>
						<img src="./pdf/pdf.png" width="80" class="img-gauche" />
						<?php
					break;
				}
				?>
				<?php echo stripslashes($donnees['description']); ?><br/>
				Taille : <?php echo filesize($donnees['lien']); ?> bytes.<br/>
				<input type="text"	 name="lien"		value="<?php echo $url.$donnees['lien']; ?>" />
				<input type="hidden" name="id" 			value="<?php echo $donnees['id']; ?>" />
				<input type="hidden" name="lien" 		value="<?php echo $donnees['lien']; ?>" /><br/>
				<input type="submit" name="editer"		value="Editer"  	class="bouton" />
				<input type="submit" name="effacer"		value="Effacer"  	class="bouton" /></p>
			</form>
			<?php 
		}
		?>
	</div>
	<?php
	$req->closeCursor();
}
